//
//  MineViewController.m
//  ChartsDemo
//
//  Created by wusiping on 2021/3/30.
//

#import "HistoryViewController.h"
// controllers
//#import "ARTOximeterHistoryDetailController.h"
//#import "ARTFetalHeartHistoryDetailController2.h"
#import "ARTWatchHistoryDailyController.h"
#import "ARTWatchHistorySleepController.h"
//#import "ARTPlateauHisDetailController.h"
// views
//#import "HistorySOP2Cell.h"
#import "UUDatePicker.h"
//#import "ARTFetalHeartHistoryCell.h"
#import "ARTWatchHistoryCell.h"
//#import "ARTPlateauHisDisView.h"
// models
#import "MeasureModel.h"
#import "ARTFetalHeartModel.h"
#import "ARTWatchHisModel.h"
//#import "ARTPelvicFloorMusclesHistoryModel.h"
//#import "ARTPelvicFloorMusclesHistoryController.h"
//#import "ARTPelvicFloorMusclesHistoryItemCell.h"
//#import "ARTPelvicFloorMusclesHistoryDetailController.h"

@interface HistoryViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UUDatePickerDelegate>

// 无数据视图
@property (nonatomic,strong) UIImageView *noDataImageView;
@property (nonatomic,strong) UILabel *noDataLabel;

@property (nonatomic,strong) UISegmentedControl *segmentControl;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *searchBgView;
@property (nonatomic,strong) UIButton *beginBtn;
@property (nonatomic,strong) UIButton *endBtn;
@property (nonatomic,strong) UIButton *queryBtn;

@property (nonatomic,strong) UIDatePicker *datePicker;
// 敬请等待
@property (nonatomic,strong) UILabel *waitLabel;

@property (nonatomic,strong) NSMutableArray<HistoryModel *> *oximeterSourceArray;
@property (nonatomic,strong) NSMutableArray<HistoryModel *> *selectOximeterArray;
@property (nonatomic,strong) NSMutableArray<ARTFetalHeartModel *> *fetalSourceArray;
@property (nonatomic,strong) NSMutableArray<ARTFetalHeartModel *> *selectFetalArray;
@property (nonatomic,strong) NSMutableArray<ARTWatchHisModel *> *dailySourceArray;
@property (nonatomic,strong) NSMutableArray<ARTWatchHisModel *> *selectDailyArray;
@property (nonatomic,strong) NSMutableArray<ARTWatchHisModel *> *sleepSourceArray;
@property (nonatomic,strong) NSMutableArray<ARTWatchHisModel *> *selectSleepArray;
@property (nonatomic,strong) NSMutableArray<HistoryModel *> *plateauSourceArray;
@property (nonatomic,strong) NSMutableArray<HistoryModel *> *selectPlateauArray;
@property (nonatomic,strong) NSMutableArray<HistoryModel *> *plateauDisArray;
/// 盆底肌历史数据
//@property (nonatomic, strong) NSMutableArray <ARTPelvicFloorMusclesHistoryItemModel *>*treatmentHisArrM;

@property (assign, nonatomic)  ARTWatchMonitorType  watchHisType;

//@property (strong, nonatomic)  ARTPlateauHisDisView *plaDisView;

@property (assign, nonatomic)  NSInteger  currentCount;

@end

@implementation HistoryViewController

#pragma mark--THE LIFE CYCLE
- (void)viewDidLoad {
    [super viewDidLoad];
    [self j_appearance];
    self.view.backgroundColor = [UIColor whiteColor];
    _currentCount = 0;
    [self UIConfig];
//    [self initDataSource];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshLanguage) name:ART_Notify_LanguageChanged object:nil];
    J_Add_Notify(self, @selector(syncFileComplete), ART_Notify_WatchSyncComplete, nil);
    
    [self reloadData];
    
    [self refresh1Languge];
}

- (void)syncFileComplete{
    [self.tableView.mj_header beginRefreshing];
}

- (void)reloadData{
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [MBProgressHUD hideHUDForView:[ARTNavigation currentNavigationViewController].view animated:YES];
    NSLog(@"离开了历史页面");
}

//- (void)initDataSource{
//    /*
//     Rehabilitation_BasinMuscle = 0, /// 盆底肌肉康复治疗
//     Rehabilitation_PelvicFloorTraining = 2, /// 盆底肌肉康复训练
//     Rehabilitation_Postpartum = 3,          /// 产后康复
//     Rehabilitation_Chest = 4,              /// 胸部康复
//     Rehabilitation_StrengthMeasurement = 1, /// 肌力测量
//     */
//    [ARTPelvicFloorHisShare selectAllData];
//    NSArray <ARTPelvicFloorMusclesHistoryDetailModel *>*basinMuscleArr = [ARTPelvicFloorHisShare selectAllDataWithTreatmentId:Rehabilitation_BasinMuscle];
//    NSArray <ARTPelvicFloorMusclesHistoryDetailModel *>*pelvicFloorArr = [ARTPelvicFloorHisShare selectAllDataWithTreatmentId:Rehabilitation_PelvicFloorTraining];
//    NSArray <ARTPelvicFloorMusclesHistoryDetailModel *>*postpartumArr = [ARTPelvicFloorHisShare selectAllDataWithTreatmentId:Rehabilitation_Postpartum];
//    NSArray <ARTPelvicFloorMusclesHistoryDetailModel *>*chestArr = [ARTPelvicFloorHisShare selectAllDataWithTreatmentId:Rehabilitation_Chest];
//    NSArray <ARTPelvicFloorMusclesHistoryDetailModel *>*strengthArr = [ARTPelvicFloorHisShare selectAllDataWithTreatmentId:Rehabilitation_StrengthMeasurement];
//    self.treatmentHisArrM = [NSMutableArray array];
//    ARTPelvicFloorMusclesHistoryItemModel *oneItem = [ARTPelvicFloorMusclesHistoryItemModel new];
//    oneItem.treatmentName = FYLocalizedString(@"pelvic_floor_records1", @"");//@"盆底肌肉康复治疗记录";
//    oneItem.treatmentCount = [NSString stringWithFormat:@"%ld%@",basinMuscleArr.count,FYLocalizedString(@"pelvic_floor_times", @"")];
//    oneItem.itemArr = basinMuscleArr;
//    oneItem.rehabilitationType = Rehabilitation_BasinMuscle;
//    [self.treatmentHisArrM addObject:oneItem];
//    
//    ARTPelvicFloorMusclesHistoryItemModel *twoItem = [ARTPelvicFloorMusclesHistoryItemModel new];
//    twoItem.treatmentName = FYLocalizedString(@"pelvic_floor_records3", @"");//@"盆底肌肉康复训练记录";
//    twoItem.treatmentCount = [NSString stringWithFormat:@"%ld%@",pelvicFloorArr.count,FYLocalizedString(@"pelvic_floor_times", @"")];
//    twoItem.itemArr = pelvicFloorArr;
//    twoItem.rehabilitationType = Rehabilitation_PelvicFloorTraining;
//    [self.treatmentHisArrM addObject:twoItem];
//    
//    ARTPelvicFloorMusclesHistoryItemModel *threeItem = [ARTPelvicFloorMusclesHistoryItemModel new];
//    threeItem.treatmentName = FYLocalizedString(@"pelvic_floor_records4", @"");//@"产后康复治疗记录";
//    threeItem.treatmentCount = [NSString stringWithFormat:@"%ld%@",postpartumArr.count,FYLocalizedString(@"pelvic_floor_times", @"")];
//    threeItem.itemArr = postpartumArr;
//    threeItem.rehabilitationType = Rehabilitation_Postpartum;
//    [self.treatmentHisArrM addObject:threeItem];
//    
//    ARTPelvicFloorMusclesHistoryItemModel *fourItem = [ARTPelvicFloorMusclesHistoryItemModel new];
//    fourItem.treatmentName = FYLocalizedString(@"pelvic_floor_records5", @"");//@"胸部康复治疗记录";
//    fourItem.treatmentCount = [NSString stringWithFormat:@"%ld%@",chestArr.count,FYLocalizedString(@"pelvic_floor_times", @"")];
//    fourItem.itemArr = chestArr;
//    fourItem.rehabilitationType = Rehabilitation_Chest;
//    [self.treatmentHisArrM addObject:fourItem];
//    
//    ARTPelvicFloorMusclesHistoryItemModel *fiveItem = [ARTPelvicFloorMusclesHistoryItemModel new];
//    fiveItem.treatmentName = FYLocalizedString(@"pelvic_floor_records2", @"");//@"肌力测量记录";
//    fiveItem.treatmentCount = [NSString stringWithFormat:@"%ld%@",strengthArr.count,FYLocalizedString(@"pelvic_floor_times", @"")];
//    fiveItem.itemArr = strengthArr;
//    fiveItem.rehabilitationType = Rehabilitation_StrengthMeasurement;
//    [self.treatmentHisArrM addObject:fiveItem];
//}

- (void)UIConfig{
    [self.view addSubview:self.segmentControl];
    [_segmentControl segmentedIOS13Style];
    [_segmentControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(36);
    }];
    [self.view addSubview:self.scrollView];
    if (_chooseDevType == ARTDeviceTypeWatch) {
        _segmentControl.hidden = NO;
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.segmentControl.mas_bottom).offset(10);
        }];
        self.watchHisType = ARTWatchMonitorTypeDaily;
    }else if (_chooseDevType == ARTDeviceTypePlateau){
        _segmentControl.hidden = YES;
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.mas_equalTo(0);
        }];
    }else if (_chooseDevType == ARTDeviceTypePelvicMuscles){
        _segmentControl.hidden = YES;
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.mas_equalTo(0);
        }];
    }else{
        _segmentControl.hidden = YES;
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.mas_equalTo(10);
        }];
    }
    
    UIView *searchBgView = [[UIView alloc] init];
    [_scrollView addSubview:searchBgView];
    _searchBgView = searchBgView;
    [searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(60);
        make.width.equalTo(self.scrollView.mas_width);
        make.left.mas_equalTo(0);
    }];
    
    _beginBtn = [[UIButton alloc] init];
    [searchBgView addSubview:_beginBtn];
    [_beginBtn setTitle:FYLocalizedString(@"oxygen_time_start", @"") forState:UIControlStateNormal];
    [_beginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(J_Width(20));
        make.centerY.equalTo(searchBgView);
        make.height.mas_equalTo(32);
        make.width.mas_equalTo(120);
    }];
    [_beginBtn addTarget:self action:@selector(touchBeginBtn) forControlEvents:UIControlEventTouchUpInside];
//    [_beginBtn setBackgroundColor:[UIColor jk_colorWithHexString:@"#F5F5F5"]];
//    _beginBtn.layer.cornerRadius = 5;
    _beginBtn.titleLabel.font = J_Font(15);
    [_beginBtn setTitleColor:J_ColorFromHEX(0x666666) forState:UIControlStateNormal];
    UIView *seperateView = [[UIView alloc] init];
    [searchBgView addSubview:seperateView];
    [seperateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(10);
        make.centerY.equalTo(searchBgView);
        make.left.equalTo(self.beginBtn.mas_right).offset(5);
    }];
    [seperateView setBackgroundColor:J_ColorFromHEX(0x666666)];
    _endBtn = [[UIButton alloc] init];
//    _endBtn.layer.cornerRadius = 5;
    _endBtn.titleLabel.font = J_Font(15);
    [_endBtn setTitleColor:J_ColorFromHEX(0x666666) forState:UIControlStateNormal];
    
    [searchBgView addSubview:_endBtn];
    [_endBtn setTitle:FYLocalizedString(@"oxygen_time_stop", @"") forState:UIControlStateNormal];
    [_endBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(seperateView.mas_right).offset(5);
        make.centerY.equalTo(searchBgView);
        make.height.mas_equalTo(32);
        make.width.mas_equalTo(120);
    }];
    [_endBtn addTarget:self action:@selector(touchEndBtn) forControlEvents:UIControlEventTouchUpInside];
//    [_endBtn setBackgroundColor:[UIColor jk_colorWithHexString:@"#F5F5F5"]];
    
    _queryBtn = [[UIButton alloc] init];
    _queryBtn.layer.cornerRadius = 5;
    _queryBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_queryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [searchBgView addSubview:_queryBtn];
    [_queryBtn setTitle:FYLocalizedString(@"oxygen_time_query", @"") forState:UIControlStateNormal];
    [_queryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.equalTo(searchBgView);
        make.height.mas_equalTo(32);
        make.width.mas_equalTo(64);
    }];
    [_queryBtn setBackgroundColor:J_ColorFromHEX(0x0095B6)];
    [_queryBtn addTarget:self action:@selector(touchQueryBtn) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.layer.cornerRadius = 4;
    self.tableView.layer.borderColor = J_ColorFromHEX(0xF1F1F1).CGColor;
    self.tableView.layer.borderWidth = 1.f;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    [self.tableView registerClass:[HistorySOP2Cell class] forCellReuseIdentifier:[HistorySOP2Cell jk_className]];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(fooderRefresh)];
    [self.scrollView addSubview:self.tableView];
    
    if (_chooseDevType == ARTDeviceTypeWatch || _chooseDevType == ARTDeviceTypePlateau) {
//        searchBgView.hidden = YES;
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.equalTo(self.scrollView.mas_left).offset(0);
            make.width.mas_equalTo(XCScreenW);
            make.bottom.equalTo(self.view);
        }];
    }else if (_chooseDevType == ARTDeviceTypePelvicMuscles ){
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(0);
            make.left.equalTo(self.scrollView.mas_left).offset(0);
            make.width.mas_equalTo(XCScreenW);
            make.bottom.equalTo(self.view);
        }];
    }else{
//        searchBgView.hidden = NO;
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_searchBgView.mas_bottom).offset(0);
            make.left.equalTo(self.scrollView.mas_left).offset(0);
            make.width.mas_equalTo(XCScreenW);
            make.bottom.equalTo(self.view);
        }];
    }
    
    UILabel *waitLabel = [[UILabel alloc] init];
    self.waitLabel = waitLabel;
    waitLabel.font = [UIFont boldSystemFontOfSize:20];
    waitLabel.text = FYLocalizedString(@"coming_soon", @"");
    waitLabel.textColor = J_ColorFromHEX(0x0095B6);
    waitLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:waitLabel];
    [waitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tableView);
        make.width.equalTo(self.tableView);
        make.left.equalTo(self.tableView.mas_right);
        
    }];
    self.noDataImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_no_data"]];
    self.noDataImageView.hidden = YES;
    [self.tableView addSubview:self.noDataImageView];
    [self.noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
    }];
    
    self.noDataLabel = [[UILabel alloc] init];
    self.noDataLabel.text = FYLocalizedString(@"no_data", @"");
    self.noDataLabel.textColor = J_ColorFromHEX(0x999999);
    self.noDataLabel.font = [UIFont systemFontOfSize:14];
    self.noDataLabel.hidden = YES;
    [self.tableView addSubview:self.noDataLabel];
    [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.noDataImageView.mas_bottom).offset(20);
    }];
    
}

#pragma mark--PUBLIC METHOD

#pragma mark--PRIVATE METHOD
- (void)headerRefresh{
    _currentCount = 0;
    [self dataConfig];
//    [self initDataSource];
}

- (void)fooderRefresh{
    [self dataConfig];
}

// 获取本地数据
- (void)dataConfig{
    [_endBtn setTitle:FYLocalizedString(@"oxygen_time_stop", @"") forState:UIControlStateNormal];
    [_beginBtn setTitle:FYLocalizedString(@"oxygen_time_start", @"") forState:UIControlStateNormal];
    
    self.oximeterSourceArray = nil;
    self.selectOximeterArray = nil;
    self.fetalSourceArray = nil;
    self.selectFetalArray = nil;
    self.dailySourceArray = nil;
    self.selectDailyArray = nil;
    self.sleepSourceArray = nil;
    self.selectSleepArray = nil;
    self.plateauSourceArray = nil;
    self.selectPlateauArray = nil;
    if (_chooseDevType == ARTDeviceTypeOximeter) {
        // 获取血氧数据
        NSArray *historys = [NSUserDefaults jk_arrayForKey:App_History];
        self.oximeterSourceArray = [HistoryModel mj_objectArrayWithKeyValuesArray:historys];
        self.selectOximeterArray = self.oximeterSourceArray;//[HistoryModel mj_objectArrayWithKeyValuesArray:historys];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }else if (_chooseDevType == ARTDeviceTypeFetalHeart){
        // 获取胎心数据
        NSArray *fetalArray = [NSUserDefaults jk_arrayForKey:App_History_FetalHeart];
        self.fetalSourceArray = [ARTFetalHeartModel mj_objectArrayWithKeyValuesArray:fetalArray];
        self.selectFetalArray = self.fetalSourceArray;//[ARTFetalHeartModel mj_objectArrayWithKeyValuesArray:fetalArray];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }else if (_chooseDevType == ARTDeviceTypeWatch){
        if (_watchHisType == ARTWatchMonitorTypeDaily) {
            // 获取手表日常数据
            NSArray *dailyArray = [NSUserDefaults jk_arrayForKey:App_History_DailyWatch];
            // 解密
            self.dailySourceArray = [@[] mutableCopy];
            for (NSString *string in dailyArray) {
                NSString *decordString = [RSAEncryptor decryptSubsectionString:string privateKey:ART_Prikey];
                ARTWatchHisModel *temp = [ARTWatchHisModel mj_objectWithKeyValues:decordString];
                [self.dailySourceArray addObject:temp];
            }
//            self.dailySourceArray = [ARTWatchHisModel mj_objectArrayWithKeyValuesArray:dailyArray];
            self.dailySourceArray = [[self.dailySourceArray sortedArrayUsingComparator:^NSComparisonResult(ARTWatchHisModel *obj1, ARTWatchHisModel *obj2) {
                NSDate *date1 = [NSDate jk_dateWithString:obj1.date format:@"yyyy/MM/dd HH:mm:ss"];
                NSDate *date2 = [NSDate jk_dateWithString:obj2.date format:@"yyyy/MM/dd HH:mm:ss"];
                return [@(date2.timeIntervalSince1970) compare:@(date1.timeIntervalSince1970)];
            }] mutableCopy];
            self.selectDailyArray = self.dailySourceArray;
        }else{
            // 获取手表睡眠数据
            NSArray *sleepArray = [NSUserDefaults jk_arrayForKey:App_History_SleepWatch];
            // 解密
            self.sleepSourceArray = [@[] mutableCopy];
            for (NSString *string in sleepArray) {
                NSString *decordString = [RSAEncryptor decryptSubsectionString:string privateKey:ART_Prikey];
                ARTWatchHisModel *temp = [ARTWatchHisModel mj_objectWithKeyValues:decordString];
                [self.sleepSourceArray addObject:temp];
            }
//            self.sleepSourceArray = [ARTWatchHisModel mj_objectArrayWithKeyValuesArray:sleepArray];
            self.sleepSourceArray = [[self.sleepSourceArray sortedArrayUsingComparator:^NSComparisonResult(ARTWatchHisModel *obj1, ARTWatchHisModel *obj2) {
                NSDate *date1 = [NSDate jk_dateWithString:obj1.date format:@"yyyy/MM/dd HH:mm:ss"];
                NSDate *date2 = [NSDate jk_dateWithString:obj2.date format:@"yyyy/MM/dd HH:mm:ss"];
                return [@(date2.timeIntervalSince1970) compare:@(date1.timeIntervalSince1970)];
            }] mutableCopy];
            self.selectSleepArray = self.sleepSourceArray;
        }
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }else if (_chooseDevType == ARTDeviceTypePlateau){
//        NSArray *plateauArray = [NSUserDefaults jk_arrayForKey:App_History];
        NSArray *plateauArray = [NSUserDefaults jk_arrayForKey:App_History_Plateau];
        NSArray *modelArray = [HistoryModel mj_objectArrayWithKeyValuesArray:plateauArray];
        _plateauDisArray = modelArray;
        NSInteger pageSize = 10;
        _currentCount = _currentCount + pageSize;
        NSMutableArray *tempArray = [NSMutableArray array];
        for (int i = 0; i < MIN(_currentCount, modelArray.count); i++) {
            [tempArray addObject:modelArray[i]];
        }
        if (tempArray.count >= modelArray.count) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        self.plateauSourceArray = tempArray;
        self.selectPlateauArray = self.plateauSourceArray;
    }
    
    [self.tableView.mj_header endRefreshing];
//    [self.tableView.mj_footer endRefreshing];
    [self reloadDataWithTableView];
}

// 刷新列表
- (void)reloadDataWithTableView{
    [self dealHideNoData];
    [self.tableView reloadData];
}

- (void)dealHideNoData{
    [self.noDataImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view);
    }];
    [self.noDataLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.noDataImageView.mas_bottom).offset(20);
    }];
    if (_chooseDevType == ARTDeviceTypeOximeter) {
        self.noDataImageView.hidden = self.noDataLabel.hidden = self.selectOximeterArray.count ? YES : NO;
    }else if (_chooseDevType == ARTDeviceTypePlateau){
        self.noDataImageView.hidden = self.noDataLabel.hidden = self.selectPlateauArray.count ? YES : NO;
        [self.noDataImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view).offset(J_SCREEN_HEIGHT/2.4/2/2);
        }];
        [self.noDataLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.noDataImageView.mas_bottom).offset(20);
        }];
    }
    else if (_chooseDevType == ARTDeviceTypeFetalHeart){
        self.noDataImageView.hidden = self.noDataLabel.hidden = self.selectFetalArray.count ? YES : NO;
    }else if (_chooseDevType == ARTDeviceTypeWatch){
        if (_watchHisType == ARTWatchMonitorTypeDaily) {
            self.noDataImageView.hidden = self.noDataLabel.hidden = self.selectDailyArray.count ? YES : NO;
        }else{
            self.noDataImageView.hidden = self.noDataLabel.hidden = self.selectSleepArray.count ? YES : NO;
        }
    }else if(_chooseDevType == ARTDeviceTypeWatch || _chooseDevType == ARTDeviceTypePlateau || _chooseDevType == ARTDeviceTypePelvicMuscles){
        [_scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
        }];
        [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(0);
        }];
    }else{
        [_scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
        }];
        [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_searchBgView.mas_top).offset(0);
        }];
    }
    if(_chooseDevType == ARTDeviceTypePelvicMuscles){
        self.noDataImageView.hidden = YES;
        self.noDataLabel.hidden = YES;
    }
}

#pragma mark--TARGET ACTION
- (void)refreshLanguage{
    [self dataConfig];
    [self refresh1Languge];
}

- (void)refresh1Languge{
    self.title = FYLocalizedString(@"history_record", @"");
//    [self initDataSource];
    [_endBtn setTitle:FYLocalizedString(@"oxygen_time_stop", @"") forState:UIControlStateNormal];
    [_beginBtn setTitle:FYLocalizedString(@"oxygen_time_start", @"") forState:UIControlStateNormal];
    
    [_queryBtn setTitle:FYLocalizedString(@"oxygen_time_query", @"") forState:UIControlStateNormal];
    _waitLabel.text = FYLocalizedString(@"coming_soon", @"");

    [self reloadDataWithTableView];
    
    [_segmentControl setTitle:[FYLocalizeableMode stringOfDailyTitle] forSegmentAtIndex:0];
    [_segmentControl setTitle:[FYLocalizeableMode stringOfSleepTitle] forSegmentAtIndex:1];
    self.noDataLabel.text = FYLocalizedString(@"no_data", @"");
}

- (void)touchBeginBtn
{
    [self setupDateKeyPanWithTag:100];
}

- (void)touchEndBtn
{
    [self setupDateKeyPanWithTag:101];
}

- (void)setupDateKeyPanWithTag:(NSInteger)tag {
    UIWindow *windows = [[UIApplication sharedApplication] keyWindow];
    UUDatePicker *calendarPickView = [[UUDatePicker alloc] initWithframe:windows.bounds Delegate:self PickerStyle:UUDateStyle_YearMonthDay];
    [windows addSubview:calendarPickView];
    [calendarPickView show];
    calendarPickView.tag = tag;
}

- (void)changeIndex{
    NSInteger index = self.segmentControl.selectedSegmentIndex;
//    self.scrollView.contentOffset = CGPointMake(index*XCScreenW, 0);
    if (index == 0) {
        self.watchHisType = ARTWatchMonitorTypeDaily;
    }else{
        self.watchHisType = ARTWatchMonitorTypeSleep;
    }
    [self.selectDailyArray removeAllObjects];
    [self.selectSleepArray removeAllObjects];
    [self.tableView reloadData];
    [self.tableView.mj_header beginRefreshing];
//    [self reloadDataWithTableView];
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    CGFloat offsetX = self.scrollView.contentOffset.x;
//    CGFloat x = offsetX/XCScreenW;
//    int index = (int)round(x);
//    self.segmentControl.selectedSegmentIndex = index;
//}

- (void)touchQueryBtn
{
    NSString *beginText = self.beginBtn.titleLabel.text;
    NSString *endText = self.endBtn.titleLabel.text;
    
    if (![beginText containsString:@"-"]) {
//        [self.view jk_makeToast:FYLocalizedString(@"oxygen_write_starttime", @"")];
        [QMUITips showWithText:FYLocalizedString(@"oxygen_write_starttime", @"")];
    }else if (![endText containsString:@"-"]) {
//        [self.view jk_makeToast:FYLocalizedString(@"oxygen_write_endtime", @"")];
        [QMUITips showWithText:FYLocalizedString(@"oxygen_write_endtime", @"")];
    }else {
        if (_chooseDevType == ARTDeviceTypeOximeter) {
            
            NSDate *beginDate = [NSDate jk_dateWithString:beginText format:@"yyyy-MM-dd"];
            NSDate *endDate = [NSDate jk_dateWithString:endText format:@"yyyy-MM-dd"];
            NSMutableArray *array = [NSMutableArray array];
            for (HistoryModel *model in self.oximeterSourceArray) {
                NSDate *tempDate = [NSDate jk_dateWithString:model.date format:@"yyyy/MM/dd HH:mm"];
                if (([tempDate jk_isLaterThanDate:beginDate] || [tempDate jk_isSameDay:beginDate]) && ([tempDate jk_isEarlierThanDate:endDate] || [tempDate jk_isSameDay:endDate])) {
                    [array addObject:model];
                }
            }
            self.selectOximeterArray = [NSMutableArray arrayWithArray:array];
        }else if (_chooseDevType == ARTDeviceTypePlateau){
            
            NSDate *beginDate = [NSDate jk_dateWithString:beginText format:@"yyyy-MM-dd"];
            NSDate *endDate = [NSDate jk_dateWithString:endText format:@"yyyy-MM-dd"];
            NSMutableArray *array = [NSMutableArray array];
            for (HistoryModel *model in self.plateauSourceArray) {
                NSDate *tempDate = [NSDate jk_dateWithString:model.date format:@"yyyy/MM/dd HH:mm"];
                if (([tempDate jk_isLaterThanDate:beginDate] || [tempDate jk_isSameDay:beginDate]) && ([tempDate jk_isEarlierThanDate:endDate] || [tempDate jk_isSameDay:endDate])) {
                    [array addObject:model];
                }
            }
            self.selectPlateauArray = [NSMutableArray arrayWithArray:array];
        }
        else if (_chooseDevType == ARTDeviceTypeFetalHeart){
            
            NSDate *beginDate = [NSDate jk_dateWithString:beginText format:@"yyyy-MM-dd"];
            NSDate *endDate = [NSDate jk_dateWithString:endText format:@"yyyy-MM-dd"];
            NSMutableArray *array = [NSMutableArray array];
            for (ARTFetalHeartModel *model in self.fetalSourceArray) {
                NSDate *tempDate = [NSDate jk_dateWithString:model.date format:@"yyyy/MM/dd HH:mm"];
                if (([tempDate jk_isLaterThanDate:beginDate] || [tempDate jk_isSameDay:beginDate]) && ([tempDate jk_isEarlierThanDate:endDate] || [tempDate jk_isSameDay:endDate])) {
                    [array addObject:model];
                }
            }
            self.selectFetalArray = [NSMutableArray arrayWithArray:array];
        }else if (_chooseDevType == ARTDeviceTypeWatch){
            if (_watchHisType == ARTWatchMonitorTypeDaily) {
                NSDate *beginDate = [NSDate jk_dateWithString:beginText format:@"yyyy-MM-dd"];
                NSDate *endDate = [NSDate jk_dateWithString:endText format:@"yyyy-MM-dd"];
                NSMutableArray *array = [NSMutableArray array];
                for (ARTWatchHisModel *model in self.dailySourceArray) {
                    NSDate *tempDate = [NSDate jk_dateWithString:model.date format:@"yyyy/MM/dd HH:mm:ss"];
                    if (([tempDate jk_isLaterThanDate:beginDate] || [tempDate jk_isSameDay:beginDate]) && ([tempDate jk_isEarlierThanDate:endDate] || [tempDate jk_isSameDay:endDate])) {
                        [array addObject:model];
                    }
                }
                self.selectDailyArray = [NSMutableArray arrayWithArray:array];
            }else{
                NSDate *beginDate = [NSDate jk_dateWithString:beginText format:@"yyyy-MM-dd"];
                NSDate *endDate = [NSDate jk_dateWithString:endText format:@"yyyy-MM-dd"];
                NSMutableArray *array = [NSMutableArray array];
                for (ARTWatchHisModel *model in self.sleepSourceArray) {
                    NSDate *tempDate = [NSDate jk_dateWithString:model.date format:@"yyyy/MM/dd HH:mm:ss"];
                    if (([tempDate jk_isLaterThanDate:beginDate] || [tempDate jk_isSameDay:beginDate]) && ([tempDate jk_isEarlierThanDate:endDate] || [tempDate jk_isSameDay:endDate])) {
                        [array addObject:model];
                    }
                }
                self.selectSleepArray = [NSMutableArray arrayWithArray:array];
            }
        }
        [self reloadDataWithTableView];
    }
}

#pragma mark--UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_chooseDevType == ARTDeviceTypeOximeter) {
        return self.selectOximeterArray.count;
    }else if (_chooseDevType == ARTDeviceTypePlateau){
        return self.selectPlateauArray.count;
    }
    else if (_chooseDevType == ARTDeviceTypeFetalHeart){
        return self.selectFetalArray.count;
    }else if (_chooseDevType == ARTDeviceTypeWatch){
        if (_watchHisType == ARTWatchMonitorTypeDaily) {
            return self.selectDailyArray.count;
        }else{
            return self.selectSleepArray.count;
        }
    }
//    else if (_chooseDevType == ARTDeviceTypePelvicMuscles){
//        return self.treatmentHisArrM.count;
//    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_chooseDevType == ARTDeviceTypeWatch) {
        return 200;
    }
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_chooseDevType == ARTDeviceTypeOximeter) {
//        HistorySOP2Cell *cell = (HistorySOP2Cell *)[self.tableView dequeueReusableCellWithIdentifier:[HistorySOP2Cell jk_className] forIndexPath:indexPath];
//        cell.model = [self.selectOximeterArray jk_objectWithIndex:indexPath.row];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        return cell;
    }else if (_chooseDevType == ARTDeviceTypePlateau){
//        HistorySOP2Cell *cell = (HistorySOP2Cell *)[self.tableView dequeueReusableCellWithIdentifier:[HistorySOP2Cell jk_className] forIndexPath:indexPath];
//        cell.topBg.backgroundColor = J_ColorFromHEX(0xE4F1FF);
//        cell.plaModel = [self.selectPlateauArray jk_objectWithIndex:indexPath.row];
//        cell.index = indexPath.row;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        return cell;
    }
    else if (_chooseDevType == ARTDeviceTypeFetalHeart){
//        ARTFetalHeartHistoryCell *cell = [ARTFetalHeartHistoryCell cellWithTableView:tableView];
//        ARTFetalHeartModel *model = [self.selectFetalArray jk_objectWithIndex:indexPath.row];
//        cell.model = model;
//        return cell;
    }else if (_chooseDevType == ARTDeviceTypeWatch){
        if (_watchHisType == ARTWatchMonitorTypeDaily) {
            ARTWatchHistoryCell *cell = [ARTWatchHistoryCell cellWithTableView:tableView];
            ARTWatchHisModel *model = [self.selectDailyArray objectAtIndex:indexPath.row];
            cell.model = model;
            return cell;
        }else{
            ARTWatchHistoryCell *cell = [ARTWatchHistoryCell cellWithTableView:tableView];
            ARTWatchHisModel *model = [self.selectSleepArray objectAtIndex:indexPath.row];
            cell.model = model;
            return cell;
        }
    }
//    else if(_chooseDevType == ARTDeviceTypePelvicMuscles){
//        ARTPelvicFloorMusclesHistoryItemCell *cell = [tableView dequeueReusableCell:ARTPelvicFloorMusclesHistoryItemCell.class];
//        ARTPelvicFloorMusclesHistoryItemModel *itemModel = [self.treatmentHisArrM jk_objectWithIndex:indexPath.row];
//        [cell dataRenderingWithItemModel:itemModel indexPath: indexPath];
//        return cell;
//    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_chooseDevType == ARTDeviceTypeOximeter) {
//        HistoryModel *model = [self.selectOximeterArray jk_objectWithIndex:indexPath.row];
//        model.lookState = 2;//已查看
//        [self storeOximeterRecord];
//        
//        ARTOximeterHistoryDetailController *controller = [[ARTOximeterHistoryDetailController alloc] init];
//        controller.model = model;
//        [ARTNavigation pushViewController:controller animated:YES];
    }
//    else if (_chooseDevType == ARTDeviceTypePlateau){
//        HistoryModel *model = [self.selectPlateauArray jk_objectWithIndex:indexPath.row];
//        model.lookState = 2;//已查看
//        [self storePlateauRecord];
//        
//        ARTPlateauHisDetailController *controller = [[ARTPlateauHisDetailController alloc] init];
//        controller.model = model;
//        [ARTNavigation pushViewController:controller animated:YES];
//    }
//    else if (_chooseDevType == ARTDeviceTypeFetalHeart){
//        ARTFetalHeartModel *model = self.selectFetalArray[indexPath.row];
//        model.lookState = 2;//已查看
//        [self storeFetalRecord];
//        
//        ARTFetalHeartHistoryDetailController2 *controller = [[ARTFetalHeartHistoryDetailController2 alloc]init];
//        controller.receiveFetalModel = model;
//        [ARTNavigation pushViewController:controller animated:YES];
//    }
    else if (_chooseDevType == ARTDeviceTypeWatch){
        if (_watchHisType == ARTWatchMonitorTypeDaily) {
            ARTWatchHisModel *model = self.selectDailyArray[indexPath.row];
            model.lookState = 2;//已查看
            [self storeDailyRecord];
            
            ARTWatchHistoryDailyController *controller = [[ARTWatchHistoryDailyController alloc]init];
            controller.receiveModel = model;
            [ARTNavigation pushViewController:controller animated:YES];
        }else{
            ARTWatchHisModel *model = self.selectSleepArray[indexPath.row];
            model.lookState = 2;//已查看
            [self storeSleepRecord];
            
            ARTWatchHistorySleepController *controller = [[ARTWatchHistorySleepController alloc]init];
            controller.receiveModel = model;
            [ARTNavigation pushViewController:controller animated:YES];
        }
    }
//    else if(_chooseDevType == ARTDeviceTypePelvicMuscles){
//        ARTPelvicFloorMusclesHistoryItemModel *itemModel = [self.treatmentHisArrM jk_objectWithIndex:indexPath.row];
//        if (itemModel.rehabilitationType == Rehabilitation_StrengthMeasurement){
//            ARTPelvicFloorMusclesHistoryDetailController *hisDetailVC = [ARTPelvicFloorMusclesHistoryDetailController new];
//            hisDetailVC.itemModel = itemModel;
//            hisDetailVC.titleStr = itemModel.treatmentName;
//            [ARTNavigation pushViewController:hisDetailVC animated:YES];
//        }else{
//            ARTPelvicFloorMusclesHistoryController *historyController = [ARTPelvicFloorMusclesHistoryController new];
//            historyController.rehabilitationType = itemModel.rehabilitationType;
//            historyController.title = itemModel.treatmentName;
//            [ARTNavigation pushViewController:historyController animated:YES];
//        }
//    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//    [tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return FYLocalizedString(@"delete", @"");
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete){
        if (_chooseDevType == ARTDeviceTypeOximeter) {
            
            HistoryModel *model = [self.selectOximeterArray objectAtIndex:indexPath.row];
            [self.oximeterSourceArray removeObject:model];
            [self.selectOximeterArray removeObject:model];
            
            [self storeOximeterRecord];
        }else if (_chooseDevType == ARTDeviceTypePlateau){
            
            HistoryModel *model = [self.selectPlateauArray objectAtIndex:indexPath.row];
            [self.plateauSourceArray removeObject:model];
            [self.selectPlateauArray removeObject:model];
            [self.plateauDisArray removeObject:model];
            
            [self storePlateauRecord];
        }
        else if (_chooseDevType == ARTDeviceTypeFetalHeart){
            ARTFetalHeartModel *model = self.selectFetalArray[indexPath.row];
            [self.fetalSourceArray removeObject:model];
            [self.selectFetalArray removeObject:model];
            
            [self storeFetalRecord];
        }else if (_chooseDevType == ARTDeviceTypeWatch){
            if (_watchHisType == ARTWatchMonitorTypeDaily) {
                ARTWatchHisModel *model = self.selectDailyArray[indexPath.row];
                [self.dailySourceArray removeObject:model];
                [self.selectDailyArray removeObject:model];
                
                [self storeDailyRecord];
            }else{
                ARTWatchHisModel *model = self.selectSleepArray[indexPath.row];
                [self.sleepSourceArray removeObject:model];
                [self.selectSleepArray removeObject:model];
                
                [self storeSleepRecord];
            }
        }
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self dealHideNoData];
//        [self.tableView.mj_header beginRefreshing];
//        [self.view jk_makeToast:FYLocalizedString(@"app_delete_succeeded", @"")];
        [QMUITips showWithText:FYLocalizedString(@"app_delete_succeeded", @"")];
//        [self.view jk_makeToast:FYLocalizedString(@"app_delete_succeeded", @"") duration:0.5 position:[NSValue valueWithCGPoint:CGPointMake(J_SCREEN_WIDTH/2, self.view.v_h-120)]];
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (_chooseDevType == ARTDeviceTypePlateau) {
////        if (self.selectPlateauArray.count == 0) {
////            return 0;
////        }
//        return J_SCREEN_HEIGHT/2.4;
//    }
//    return 0;
//}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    _plaDisView = [[ARTPlateauHisDisView alloc]initWithFrame:J_FrameWithSize(tableView.v_w, J_SCREEN_HEIGHT/2.4)];
//    _plaDisView.plateauHisArray = _plateauDisArray;
//    return _plaDisView;
//}

// 保存修改后的胎心记录
- (void)storeFetalRecord{
    NSMutableArray *storeArray = [NSMutableArray array];
    for (ARTFetalHeartModel *model in self.fetalSourceArray) {
        [storeArray addObject:[model mj_JSONObject]];
    }
    [NSUserDefaults jk_setObject:storeArray forKey:App_History_FetalHeart];
}

// 保存修改后的血氧数据
- (void)storeOximeterRecord{
    NSMutableArray *storeArray = [NSMutableArray array];
    for (HistoryModel *model in self.oximeterSourceArray) {
        [storeArray addObject:[model mj_JSONObject]];
    }
    [NSUserDefaults jk_setObject:storeArray forKey:App_History];
}

- (void)storePlateauRecord{
    NSMutableArray *storeArray = [NSMutableArray array];
    for (HistoryModel *model in self.plateauDisArray) {
        [storeArray addObject:[model mj_JSONObject]];
    }
    [NSUserDefaults jk_setObject:storeArray forKey:App_History_Plateau];
}

- (void)storeDailyRecord{
    NSMutableArray *storeArray = [NSMutableArray array];
    for (ARTWatchHisModel *model in self.dailySourceArray) {
        NSString *dict = [model mj_JSONString];
        dict = [RSAEncryptor encryptSubsectionString:dict publicKey:ART_Pubkey];
        [storeArray addObject:dict];
//        [storeArray addObject:[model mj_JSONObject]];
    }
    [NSUserDefaults jk_setObject:storeArray forKey:App_History_DailyWatch];
}

- (void)storeSleepRecord{
    NSMutableArray *storeArray = [NSMutableArray array];
    for (ARTWatchHisModel *model in self.sleepSourceArray) {
        NSString *dict = [model mj_JSONString];
        dict = [RSAEncryptor encryptSubsectionString:dict publicKey:ART_Pubkey];
        [storeArray addObject:dict];
//        [storeArray addObject:[model mj_JSONObject]];
    }
    [NSUserDefaults jk_setObject:storeArray forKey:App_History_SleepWatch];
}

#pragma mark - UUDatePickerDelegate
- (void)uuDatePicker:(UUDatePicker *)datePicker year:(NSString *)year month:(NSString *)month day:(NSString *)day hour:(NSString *)hour minute:(NSString *)minute weekDay:(NSString *)weekDay{
    NSString *yearStr = [year substringWithRange:NSMakeRange(0, year.length-1)];
    NSString *monthStr = [month substringWithRange:NSMakeRange(0, month.length-1)];
    NSString *dayStr = [day substringWithRange:NSMakeRange(0, day.length-1)];
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@",yearStr,monthStr,dayStr];
    if (datePicker.tag == 100) {
        [self.beginBtn setTitle:dateStr forState:UIControlStateNormal];
    }else {
        [self.endBtn setTitle:dateStr forState:UIControlStateNormal];

    }
}

#pragma mark--SETTER
- (void)setChooseDevType:(ARTDeviceType)chooseDevType{
    _chooseDevType = chooseDevType;
    _currentCount = 0;
    [_tableView.mj_footer resetNoMoreData];
    _segmentControl.hidden = YES;
    _tableView.backgroundColor = [UIColor whiteColor];
    if (chooseDevType == ARTDeviceTypePlateau || chooseDevType == ARTDeviceTypeOximeter) {
        _tableView.backgroundColor = J_ColorFromHEX(0xFBFBFB);
    }
    if (chooseDevType == ARTDeviceTypeWatch) {
        _segmentControl.hidden = NO;
        [_scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.segmentControl.mas_bottom).offset(10);
        }];
        [_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.equalTo(self.scrollView.mas_left).offset(0);
            make.width.mas_equalTo(XCScreenW);
            make.bottom.equalTo(self.view);
        }];
        self.watchHisType = ARTWatchMonitorTypeDaily;
        self.segmentControl.selectedSegmentIndex = 0;
    }else if (chooseDevType == ARTDeviceTypePlateau){
        [_scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.mas_equalTo(0);
//            make.top.equalTo(self.segmentControl.mas_bottom).offset(10);
        }];
        [_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.equalTo(self.scrollView.mas_left).offset(0);
            make.width.mas_equalTo(XCScreenW);
            make.bottom.equalTo(self.view);
        }];
    }
    else{
        [_scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.mas_equalTo(10);
        }];
        [_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_searchBgView.mas_bottom).offset(0);
            make.left.equalTo(self.scrollView.mas_left).offset(0);
            make.width.mas_equalTo(XCScreenW);
            make.bottom.equalTo(self.view);
        }];
    }
    [self.tableView.mj_header beginRefreshing];
    [_beginBtn setTitle:FYLocalizedString(@"oxygen_time_start", @"") forState:UIControlStateNormal];
    [_endBtn setTitle:FYLocalizedString(@"oxygen_time_stop", @"") forState:UIControlStateNormal];
    [self reloadDataWithTableView];
}

#pragma mark--GETTER
- (UIScrollView *)scrollView{
    if (_scrollView==nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.contentSize = CGSizeMake(XCScreenW, 0);
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator=NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UISegmentedControl *)segmentControl{
    if (!_segmentControl) {
        _segmentControl = [[UISegmentedControl alloc] initWithItems:@[FYLocalizedString(@"Daily report", @""),FYLocalizedString(@"Sleep report", @"")]];
        _segmentControl.tintColor = J_ColorFromHEX(0x0095B6);
        _segmentControl.selectedSegmentIndex = 0;
        [_segmentControl addTarget:self action:@selector(changeIndex) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentControl;
}

@end
